package com.qnvip.sbd.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static String composeString(Collection<?> collection) {
		if (collection == null || collection.size() == 0) {
			return null;
		}
		StringBuilder str = new StringBuilder();
		collection.forEach(tmp -> str.append(",").append(tmp));
		return str.substring(1);
	}

	/**
	 * 首字母大写
	 * 
	 * <pre>
	 * StringUtils.uperFirst("hello") => Hello
	 * StringUtils.uperFirst("") => ""
	 * StringUtils.uperFirst(null) => ""
	 * StringUtils.uperFirst("helloWorld") => HelloWorld
	 * </pre>
	 * 
	 * @param str
	 * 
	 */
	public static String upperFirst(String str) {
		if (StringUtils.isEmpty(str))
			return StringUtils.EMPTY;
		return StringUtils.upperCase(String.valueOf(str.charAt(0))) + StringUtils.substring(str, 1);
	}
	
	/**
	 * 将list中对象的指定字段取出用分隔符连接组成字符串
	 * 
	 * @param list
	 * @param column
	 *            指定字段
	 * @param separator
	 *            分隔符
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static String getColumnValue(List<?> list, String column, String separator)
			throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		if (list == null || list.size() == 0)
			return null;
		StringBuilder sb = new StringBuilder();
		Method m = null;
		for (Object object : list) {
			m = object.getClass().getMethod("get" + upperFirst(column));
			sb.append(separator).append(m.invoke(object));
		}
		return sb.substring(1);
	}

	public static String getColumnValue(List<?> list, String column)
			throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		return getColumnValue(list, column, ",");
	}

	/**
	 * 将list中对象的指定字段取出用分隔符连接组成字符串
	 * 
	 * @param list
	 * @param column
	 *            指定字段
	 * @param separator
	 *            分隔符
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static String getNestedColumnValue(List<?> list, String column)
			throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		return getNestedColumnValue(list, column, ",");
	}
	
	/**
	 * 将list中对象的指定字段取出用分隔符连接组成字符串
	 * 
	 * @param list
	 * @param column
	 *            指定字段
	 * @param separator
	 *            分隔符
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static String getNestedColumnValue(List<?> list, String column, String separator)
			throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException {
		if (list == null || list.size() == 0)
			return null;
		StringBuilder sb = new StringBuilder();
		for (Object object : list) {
			sb.append(separator).append(getNestedValue(object, column, 0));
		}
		return sb.substring(1);
	}
	
	private static Object getNestedValue(Object object, String column, int depth) throws NoSuchMethodException, SecurityException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		String[] columns = column.split("\\.");
		Method m = object.getClass().getMethod("get" + upperFirst(columns[depth]));
		Object target = m.invoke(object);
		if (depth == (columns.length - 1))
			return target;
		else
			return getNestedValue(target, column, ++depth);
	}
	
	/**
	 * 获取指定长度的随机数字、字母字符串
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		StringBuffer buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz");
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = buffer.length();
		for (int i = 0; i < length; i++) {
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	
	/**
	 * 获取指定长度的随机数字字符串
	 * @param length
	 * @return
	 */
	public static String getNumberRandom(int length) {
		StringBuffer buffer = new StringBuffer("0123456789");
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = buffer.length();
		for (int i = 0; i < length; i++) {
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	
	/**
	 * 前面补0, 补足指定长度
	 * 
	 * @param source
	 * @param length 位数
	 * @return
	 */
	public static String zeroFill(int source, int length) {
		return String.format("%0" + length + "d", source);
	}
	
	/**
	 * 字符串补齐
	 * @param source 源字符串
	 * @param fillLength 补齐长度
	 * @param fillChar 补齐的字符
	 * @param isLeftFill true为左补齐，false为右补齐
	 * @return
	 */
	public static String stringFill(String source, int length, char fillChar, boolean isLeftFill) {
	    if (source == null || source.length() >= length) return source;
	     
	    StringBuilder result = new StringBuilder(length);
	    int len = length - source.length();
	    if (isLeftFill) {
	        for (; len > 0; len--) {
	            result.append(fillChar);
	        }
	        result.append(source);
	    } else {
	        result.append(source);
	        for (; len > 0; len--) {
	            result.append(fillChar);
	        }
	    }
	    return result.toString();
	}
	
	/**
	 * 在字符串左边补齐指定字符
	 */
	public static String stringFill(String source, int length, char fillChar) {
		return stringFill(source, length, fillChar, true);
	}
	
	/**
	 * 字符串转换unicode
	 * 
	 * @param string
	 * @return
	 */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    /**
     * unicode 转字符串
     * 
     * @param unicode
     * @return
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int data = Integer.parseInt(hex[i], 16);
            string.append((char) data);
        }
        return string.toString();
    }
    
    public static final char UNDERLINE = '_';

    /**
     * 驼峰转下划线
     * 
     * @param param
     * @return
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     * 
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        StringBuilder sb = new StringBuilder(param);
        Matcher mc = Pattern.compile("_").matcher(param);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }
}
